<template>
	<view class="container">
		<view class="logo-area">
			<img class="logo-sty" :src="logoUrl" alt="">
			<text class="title">昆山高新区篮球协会</text>
		</view>
		<view class="banner-area">
			<uni-swiper-dot class="uni-swiper-dot-box" @clickItem="clickItem" :info="info" :current="current"
				:mode="mode" :dots-styles="dotsStyles" field="content">
				<swiper class="swiper-box" @change="change" :current="swiperDotIndex" autoplay circular>
					<swiper-item v-for="(item, index) in info" :key="index">
						<img style="width: 100%;height: 380rpx;" :src="item.url" alt="">
						<view>{{item.content}}</view>
					</swiper-item>
				</swiper>
			</uni-swiper-dot>
		</view>
		<view class="nav-tab">
			<view v-for="(item,index) in navList" class="nav-sty" :key="index" @click="toPage(item)">
				<img :src="item.iconUrl" alt="" class="nav-img">
				<view class="nav-title">{{item.navTitle}}</view>
			</view>
		</view>
		<view class="register-area">
			<view class="reg reg-l" @click="toRegPage"></view>
			<view class="reg reg-r" @click="toBallTeamPage"></view>
		</view>
		<view class="about-area">
			<view class="about-tt">协会介绍</view>
			<view v-html="aboutMeInfo" style="font-size: 14px;margin-top: 18px;"></view>
		</view>
	</view>
</template>

<script>
	const db = uniCloud.database();
	const dbCmd = db.command;
	import {
		filterArticles
	} from '@/utils/baseFn.js';
	
	function checkOutVS () {
		const updateManager = wx.getUpdateManager()
		
		updateManager.onCheckForUpdate(function (res) {
		  // 请求完新版本信息的回调
		  console.log(res.hasUpdate)
		})
		
		updateManager.onUpdateReady(function () {
		  wx.showModal({
		    title: '更新提示',
		    content: '新版本已经准备好，是否重启应用？',
			showCancel: false,
		    success: function (res) {
		      if (res.confirm) {
		        // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
		        updateManager.applyUpdate()
		      }
		    }
		  })
		})
		
		updateManager.onUpdateFailed(function () {
		  // 新版本下载失败
		})
	}
	export default {
		data() {
			return {
				logoUrl: '',
				info: [],
				modeIndex: -1,
				styleIndex: -1,
				current: 0,
				mode: 'default',
				dotsStyles: {
					backgroundColor: '#fce9b1',
					border: '1px #fce9b1 solid',
					color: '#fff',
					selectedBackgroundColor: '#f9b76d',
					selectedBorder: '1px #f9b76d solid'
				},
				swiperDotIndex: 0,
				navList: [{
						iconUrl: '/static/nav/xhdt.png',
						navTitle: '协会动态',
						id: 0
					},
					{
						iconUrl: '/static/nav/ssyg.png',
						navTitle: '赛事预告',
						id: 1
					},
					{
						iconUrl: '/static/nav/jchf.png',
						navTitle: '精彩回放',
						id: 2
					},
					{
						iconUrl: '/static/nav/more.png',
						navTitle: '更多',
						id: 3
					}
				],
				aboutMeInfo: "",
				userInfo: null,
				where: `"deleted_at"==null`,
				loginTime: null,
				ballTeamStats:null
			}
		},
		onLoad () {
			checkOutVS()
		},
		onReady() {
			this.getBannerList()
			this.getAboutMe()
			this.logoUrl = `${this.$projectImg}logo.png`
		},
		onShow() {
			const _this = this
			const info = wx.getSystemInfoSync()
			const formDay = function (time) {
				const day = new Date(time)
				return `${day.getFullYear()}-${day.getMonth()}-${day.getDate()}`
			}
			let str = ''
			uni.getStorage({
				key: 'userInfo',
				success: function(res) {
					console.log(res.data)
					if (res.data) {
						_this.userInfo = res.data
						_this.checkBallTeam(res.data._id)
						const bn = _this.userInfo.ballTeamName||''
						const ut = _this.userInfo.userType == 0 ? '会员':_this.userInfo.userType == 1?'球员':'管理员'
						str = `亲爱的${bn}${ut},欢迎您的归来！`
					}
				}
			})
			uni.getStorage({
				key:'loginTime',
				success: function (res) {
					_this.loginTime = res.data
					let nd = null
					let lt = null
					if (_this.loginTime) {
						nd = formDay(new Date().getTime())
						lt = formDay(_this.loginTime)
					}
					if (nd == lt) {
						uni.showModal({
							content: str,
							showCancel: false
						})
						uni.setStorage({
							key: 'loginTime',
							data: _this.loginTime + 3600*24*1000,
							success: function () {}
						})
					}
				},
				fail: function (res) {
					console.log(res)
					uni.showModal({
						content: str||'欢迎您的到来！',
						showCancel: false
					})
					uni.setStorage({
						key: 'loginTime',
						data: new Date().getTime() + 3600*24*1000,
						success: function () {}
					})
				}
			})
			this.glabalData = {
			    screenWidth: info.screenWidth,
			    screenHeight: info.screenHeight
			}
		},
		mounted() {},
		methods: {
			getBannerList() {
				db.collection('advertising-MGMT').field("annexId,_id").get().then((res) => {
					const data = res.result.data
					if (data) {
						let imgs = data[0].annexId.split('+')
						for (let i = 0; i < imgs.length; i++) {
							const img = imgs[i].split(',')
							this.info.push({
								fileID: `${this.$baseImgUrl}${img[0]}`,
								url: `${this.$baseImgUrl}${img[0]}`,
								name: img[1]
							})
						}
						console.log(this.info)
					}
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
			},
			getAboutMe() {
				uni.showLoading({
					mask: true
				})
				db.collection('news-MGMT').orderBy('updated_date desc').where(`"deleted_at"==null&&"isWXHomeShow"=="1"`).field(
					"title,introduce,deleted_at").get().then((res) => {
					const data = res.result.data[0]
					if (data) {
						this.aboutMeInfo = filterArticles(data.introduce, this.glabalData)
					}
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
			},
			checkBallTeam (userId) {
				db.collection('team-MGMT').where({
					createdUserId: userId
				}).field("teamName,state").get().then((tre) => {
					this.ballTeamStats = tre.result.data[0]
				})
			},
			change(e) {
				this.current = e.detail.current
			},
			clickItem(e) {
				this.swiperDotIndex = e
			},
			onBanner(index) {
				console.log(22222, index);
			},
			toPage(item) {
				if (this.userInfo) {
					let url
					if (item.id == 3) {
						uni.showModal({
							content: '更多功能筹划中......',
							showCancel: false
						})
						return
					}
					if (item.id == 0) {
						url = '../news/news'
					}
					if (item.id == 1) {
						url = '../competition/competition'
					}
					if (item.id == 2) {
						url = '../playBack/playBack'
					}
					uni.navigateTo({
						url: url
					})
				} else {
					uni.navigateTo({
						url: '../login/login'
					});
				}
			},
			toRegPage() {
				if (this.userInfo) {
					if (this.ballTeamStats) {
						let str
						if (this.ballTeamStats.state == 0) {
							str = `待审批，请耐性等待或联系管理员！`
						}
						if (this.ballTeamStats.state == 1) {
							str = '已通过，一个账号只能申请一个球队！'
						}
						if (this.ballTeamStats.state == 2) {
							str = '被驳回，请至个人中心-我的球队中查看或修改！'
						}
						uni.showModal({
							content: `您注册的球队,[${this.ballTeamStats.teamName}]${str}`,
							showCancel: false
						})
					} else {
						uni.navigateTo({
							url: '../register/ballTeamSelect'
						});
					}
				} else {
					uni.navigateTo({
						url: '../login/login'
					});
				}
			},
			toBallTeamPage() {
				if (this.userInfo) {
					uni.navigateTo({
						url: '../ballTeam/ballTeam'
					});
				} else {
					uni.navigateTo({
						url: '../login/login'
					});
				}
			}
		}
	}
</script>

<style>
	.container {
		height: 100vh;
		background-color: #f6f7fb;
	}

	.logo-area {
		padding: 10rpx;
	}

	.logo-area .logo-sty {
		width: 64rpx;
		height: 64rpx;
		vertical-align: middle;
		margin-right: 20rpx;
	}

	.logo-area .title {
		font-size: 36rpx;
		font-weight: 600;
	}

	.banner-area {
		width: 100%;
		height: 190px;
	}

	.nav-tab {
		margin-top: 10rpx;
		display: flex;
		text-align: center;
	}

	.nav-sty {
		flex: 1;
	}

	.nav-img {
		width: 60px;
		height: 60px;
		border-radius: 30px;
	}

	.nav-title {
		font-size: 26rpx;
	}

	.register-area {
		padding: 32rpx;
		height: 130px;
	}

	.reg {
		width: 49%;
		height: 95px;
		display: inline-block;
	}

	.reg-l {
		background: url('../../../static/xhrk-l.jpg') no-repeat 0 0;
		background-size: 100% 95px;
		float: left;
	}

	.reg-r {
		background: url('../../../static/qdfc-r.jpg') no-repeat 0 0;
		background-size: 100% 95px;
		float: right;
	}

	.about-area {
		margin: 0 32rpx 32rpx;
		background-color: #ffffff;
		border-radius: 5px;
		min-height: 230px;
		padding: 32rpx;
	}

	.about-tt {
		font-size: 28rpx;
		font-weight: 500;
	}
</style>
<style lang="scss">
	.swiper-box {
		height: 190px;
	}
</style>